/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.sf.l2j.gameserver.skills.effects;

import java.util.Collection;
import java.util.List;

import javolution.util.FastList;
import net.sf.l2j.gameserver.ai.CtrlIntention;
import net.sf.l2j.gameserver.model.CharEffectList;
import net.sf.l2j.gameserver.model.L2Effect;
import net.sf.l2j.gameserver.model.L2Object;
import net.sf.l2j.gameserver.model.actor.L2Attackable;
import net.sf.l2j.gameserver.model.actor.L2Character;
import net.sf.l2j.gameserver.model.actor.L2Playable;
import net.sf.l2j.gameserver.model.actor.instance.L2ChestInstance;
import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
import net.sf.l2j.gameserver.skills.Env;
import net.sf.l2j.gameserver.templates.skills.L2EffectType;
import net.sf.l2j.util.Rnd;

/**
 * This effect changes the target of the victim. It adds some random aggro aswell to force the monster
 * to keep attacking. As the added aggro is random, the victim can often change of target.<br><br>
 * Any character can fill the aggroList of the victim. For a specialized use, consider using
 * EffectConfuseMob.
 * 
 * @author littlecrow, Tryskell
 */
public class EffectConfusion extends L2Effect 
{
	public EffectConfusion(Env env, EffectTemplate template)
	{
		super(env, template);
	}

	@Override
	public L2EffectType getEffectType()
	{
		return L2EffectType.CONFUSION;
	}

	/** Notify started */
	@Override
	public boolean onStart() 
	{
		getEffected().startConfused();
		onActionTime();
		return true;
	}

	/** Notify exited */
	@Override
	public void onExit() 
	{
		getEffected().stopConfused(this);
	}

    @Override
	public boolean onActionTime()
    {
		List<L2Character> targetList = new FastList<L2Character>();

		// Getting the possible targets
		Collection<L2Object> objs = getEffected().getKnownList().getKnownObjects().values();
        for (L2Object obj : objs)
        {
        	// Attackable NPCs and playable characters (players, summons) are put in the list.
            if ((obj instanceof L2Attackable || obj instanceof L2Playable) && (obj != getEffected()))
            	// Don't put doors nor chests on it.
            	if (!(obj instanceof L2DoorInstance || obj instanceof L2ChestInstance))
            		targetList.add((L2Character)obj);
        }
        
		// if there is no target, exit function
		if (targetList.isEmpty())
			return true;

		// Choosing randomly a new target
		int nextTargetIdx = Rnd.nextInt(targetList.size());
		L2Object target = targetList.get(nextTargetIdx);

		// Attacking the target
		getEffected().setTarget(target);
		getEffected().getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
		
		// Add aggro to that target aswell. The aggro power is random.
		int aggro = (5 + Rnd.get(5)) * getEffector().getLevel();
		((L2Attackable)getEffected()).addDamageHate((L2Character)target, 0, aggro);
		
    	return true;
    }

	@Override
	public int getEffectFlags()
	{
		return CharEffectList.EFFECT_FLAG_CONFUSED;
	}
}